/*
 * TaskJPanel.java
 *
 * Created on 18 de julio de 2008, 12:14
 */
package com.cartiec.jrenamer;

import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.AbstractListModel;

/**
 *
 * @author  Tonny
 */
public class TaskJPanel extends javax.swing.JPanel {

    private LstModel lstModel = new LstModel();
    private HashMap<String, String> tasksDescriptions;
    private ArrayList<String> tasksList;

    /** Creates new form TaskJPanel */
    public TaskJPanel() {
        initComponents();
    }

    public void setTasksDescriptions(HashMap<String, String> tasks) {
        this.tasksDescriptions = tasks;        
    }

    public void setData(ArrayList<String> lst) {
        tasksList = lst;
        this.lst.setModel(new LstModel());                
    }

    class LstModel extends AbstractListModel {
        public int getSize() {
            return tasksList.size();
        }

        public String getElementAt(int i) {
            return tasksDescriptions.get(tasksList.get(i));
        }

        public ArrayList<String> getList() {
            return tasksList;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        lst = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        btnFirst = new javax.swing.JButton();
        btnPrevious = new javax.swing.JButton();
        btnNext = new javax.swing.JButton();
        btnLast = new javax.swing.JButton();

        setBorder(new javax.swing.border.MatteBorder(new javax.swing.ImageIcon(getClass().getResource("/com/cartiec/jrenamer/res/emblem-new.png")))); // NOI18N
        setLayout(new java.awt.BorderLayout(3, 3));

        lst.setModel(lstModel);
        jScrollPane1.setViewportView(lst);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.setPreferredSize(new java.awt.Dimension(34, 152));
        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.Y_AXIS));

        btnFirst.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/cartiec/jrenamer/res/icons22x22/go-top.png"))); // NOI18N
        btnFirst.setPreferredSize(new java.awt.Dimension(24, 24));
        btnFirst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFirstActionPerformed(evt);
            }
        });
        jPanel1.add(btnFirst);

        btnPrevious.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/cartiec/jrenamer/res/icons22x22/go-up.png"))); // NOI18N
        btnPrevious.setPreferredSize(new java.awt.Dimension(24, 24));
        btnPrevious.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPreviousActionPerformed(evt);
            }
        });
        jPanel1.add(btnPrevious);

        btnNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/cartiec/jrenamer/res/icons22x22/go-down.png"))); // NOI18N
        btnNext.setPreferredSize(new java.awt.Dimension(24, 24));
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });
        jPanel1.add(btnNext);

        btnLast.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/cartiec/jrenamer/res/icons22x22/go-bottom.png"))); // NOI18N
        btnLast.setPreferredSize(new java.awt.Dimension(24, 24));
        btnLast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLastActionPerformed(evt);
            }
        });
        jPanel1.add(btnLast);

        add(jPanel1, java.awt.BorderLayout.EAST);
    }// </editor-fold>//GEN-END:initComponents

private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFirstActionPerformed
    int i = lst.getSelectedIndex();
    if (i >= 0) {
        String v = tasksList.remove(i);
        tasksList.add(0, v);
        paint();
    }
}//GEN-LAST:event_btnFirstActionPerformed

private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPreviousActionPerformed
    int i = lst.getSelectedIndex();
    if (i >= 0) {
        if (i - 1 >= 0) {
            String v = tasksList.remove(i);
            tasksList.add(i - 1, v);
            paint();
        }
    }
}//GEN-LAST:event_btnPreviousActionPerformed

private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
    int i = lst.getSelectedIndex();
    if (i >= 0) {
        int l = tasksList.size();
        if (i + 1 <= l) {
            String v = tasksList.remove(i);
            tasksList.add(i + 1, v);
            paint();
        }
    }
}//GEN-LAST:event_btnNextActionPerformed

private void btnLastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLastActionPerformed
    int i = lst.getSelectedIndex();
    if (i >= 0) {
        String v = tasksList.remove(i);
        int l = tasksList.size();
        tasksList.add(l, v);
        paint();
    }
}//GEN-LAST:event_btnLastActionPerformed


    public  void paint(){
        if(lst.getGraphics() != null){
            lst.paint(lst.getGraphics());
        }
        System.out.println(lst.getComponentCount() + " " + tasksList.size());
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFirst;
    private javax.swing.JButton btnLast;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrevious;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lst;
    // End of variables declaration//GEN-END:variables
}
